<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" type="text/css" href="public/css.css"> 

<h1>Huynh Trong Nhan</h1>
<?php
require_once "./model/db3.php";
$query = "SELECT * FROM pages";
$pages = getData($query);

$query = "SELECT * FROM companies";
$companies = getData($query);
$query = "SELECT * FROM apps";
$apps = getData($query);
?>
<form method="POST">
    Min <input type="text" name="min_fans"><br>
    Max <input type="text" name="max_fans"><br>
    Keyword <input type="text" name="keyword"><br>
    Company<select name="id_company">
        <option value="0">Company</option>
        <?php foreach ($companies as $company): ?>S
            <option value="<?php echo $company['id'] ?>"><?php echo $company['name'] ?></option>
        <?php endforeach; ?>
    </select><br>

    Applications<select name="id_apps">
        <option value="0">Apps</option>
        <?php foreach ($apps as $app): ?>
            <option value="<?php echo $app['id'] ?>"><?php echo $app['name'] ?></option>
        <?php endforeach; ?>
    </select><br>

    <input type="submit" name="submit" value="Tim Kiem">
</form>

<?php if (isset($_POST['submit'])): ?>
    <?php
    $min_fans = $_POST['min_fans'];
    $max_fans = $_POST['max_fans'];
    $keyword = $_POST['keyword'];
    $company = $_POST["id_company"];
    $apps = $_POST["id_apps"];
    if (!empty($company))
        $company = "(companies.id=$company) AND";
    else
        $company = "";
    if (!empty($apps))
        $apps = "(apps.id=$apps) AND";
    else
        $apps = "";



    if (empty($max_fans) && !empty($min_fans)) {
        $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps) 
                  (pages.likes>= $min_fans) and
                  (pages.description LIKE) '% $keyword %'
                  LIMIT 0,10";
    } else if (!empty($max_fans) && empty($min_fans)) {
        $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                  (pages.likes<= $max_fans) and
                  (pages.description LIKE) '% $keyword %'
                  LIMIT 0,10";
    } else if (empty($max_fans) && empty($min_fans)) {
        $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                  pages.description LIKE '% $keyword %'
                  LIMIT 0,10";
    } else {
        $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
            where $company
                  $apps
                  (pages.likes>= $min_fans) and
                  (pages.likes<= $max_fans) and
                  pages.description LIKE '% $keyword %'
                  LIMIT 0,10";
    }


    var_dump($query);

    $pages = getData($query);
    ?>
    <?php if (!empty($pages)): ?>
        <table border ='2'>
            <tr>
                <th> Picture </th>
                <th> Likes </th>
                <th> ID </th>
                <th> Name </th>
                <th> User name </th>
                <th> Description </th>
                <th> Link </th>
                <th> Street </th>
                <th> City </th>
                <th> Country </th>
            </tr>
        <?php foreach ($pages as $page): ?> 
                <tr>
                    <td><img src = '<?php echo $page["picture"] ?>'></td>
                    <td><?php echo $page["likes"] ?></td>
                    <td><?php echo $page["id"] ?></td>
                    <td><?php echo $page["name"] ?></td>
                    <td><?php echo $page["username"] ?></td>
                    <td><?php echo substr($page["description"], 0, 80) ?></td>
                    <td><?php echo $page["link"] ?></td>
                    <td><?php echo $page["street"] ?></td>
                    <td><?php echo $page["city"] ?></td>
                    <td><?php echo $page["country"] ?></td>
                </tr>
        <?php endforeach; ?>
        </table>
    <?php endif; ?>
<?php endif; ?>
